/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

import Database.ConnectToDatabase;
import java.sql.*;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author PhucTakua
 */
public class Employee {

    private int idemployee;
    private String nameemployee;
    private String username;
    private String password;
    private String sex;
    private String homeaddress;
    private String emailaddress;
    private String phonenumber;
    private java.sql.Date birthday;
    private int iddepartment;

    public Employee() {
    }

    /**
     * @return the idemployee
     */
    public int getIdemployee() {
        return idemployee;
    }

    /**
     * @param idemployee the idemployee to set
     */
    public void setIdemployee(int idemployee) {
        this.idemployee = idemployee;
    }

    /**
     * @return the nameemployee
     */
    public String getNameemployee() {
        return nameemployee;
    }

    /**
     * @param nameemployee the nameemployee to set
     */
    public void setNameemployee(String nameemployee) {
        this.nameemployee = nameemployee;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the sex
     */
    public String getSex() {
        return sex;
    }

    /**
     * @param sex the sex to set
     */
    public void setSex(String sex) {
        this.sex = sex;
    }

    /**
     * @return the homeaddress
     */
    public String getHomeaddress() {
        return homeaddress;
    }

    /**
     * @param homeaddress the homeaddress to set
     */
    public void setHomeaddress(String homeaddress) {
        this.homeaddress = homeaddress;
    }

    /**
     * @return the emailaddress
     */
    public String getEmailaddress() {
        return emailaddress;
    }

    /**
     * @param emailaddress the emailaddress to set
     */
    public void setEmailaddress(String emailaddress) {
        this.emailaddress = emailaddress;
    }

    /**
     * @return the phonenumber
     */
    public String getPhonenumber() {
        return phonenumber;
    }

    /**
     * @param phonenumber the phonenumber to set
     */
    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }

    /**
     * @return the birthday
     */
    public java.sql.Date getBirthday() {
        return birthday;
    }

    /**
     * @param birthday the birthday to set
     */
    public void setBirthday(java.sql.Date birthday) {
        this.birthday = birthday;
    }

    /**
     * @return the iddepartment
     */
    public int getIddepartment() {
        return iddepartment;
    }

    /**
     * @param iddepartment the iddepartment to set
     */
    public void setIddepartment(int iddepartment) {
        this.iddepartment = iddepartment;
    }

    public boolean AddEmployee() {
        return CommonQuery.Command("insert into employee values('" + this.nameemployee + "','" + this.username + "','" + this.password + "', '" + this.sex + "','" + this.homeaddress + "','" + this.emailaddress + "', '" + this.phonenumber + "', '" + this.birthday + "','" + this.iddepartment + "' )");
    }

    public boolean UpdateEmployee() {
        return CommonQuery.Command("update employee set nameemployee ='" + this.nameemployee + "',username='" + this.username + "',password='" + this.password + "',sex='" + this.sex + "',homeaddress='" + this.homeaddress + "',emailaddress='" + this.emailaddress + "',phonenumber='" + this.phonenumber + "',birthday='" + this.birthday + "',iddepartment=" + this.iddepartment + " where idemployee=" + this.idemployee);
    }

    public boolean DeleteEmployee() {
        return CommonQuery.Command("delete employee where idemployee=" + this.idemployee);
    }

    public static Vector<Employee> ListIDE() {
        Vector<Employee> liste = new Vector<Employee>();
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from employee order by idemployee desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Employee em = new Employee();
                em.setIdemployee(rs.getInt("idemployee"));
                em.setNameemployee(rs.getString("nameemployee"));
                em.setUsername(rs.getString("username"));
                em.setPassword(rs.getString("password"));
                em.setSex(rs.getString("sex"));
                em.setHomeaddress(rs.getString("homeaddress"));
                em.setEmailaddress(rs.getString("emailaddress"));
                em.setPhonenumber(rs.getString("phonenumber"));
                em.setBirthday(rs.getDate("birthday"));
                em.setIddepartment(rs.getInt("iddepartment"));
                liste.add(em);
            }
        } catch (Exception ex) {
            liste = null;
        }
        return liste;
    }

    public static Vector<Employee> ListIDONID(int a) {
        Vector<Employee> liste = new Vector<Employee>();
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from employee where idemployee=" + a;
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Employee em = new Employee();
                em.setIdemployee(rs.getInt("idemployee"));
                em.setNameemployee(rs.getString("nameemployee"));
                em.setUsername(rs.getString("username"));
                em.setPassword(rs.getString("password"));
                em.setSex(rs.getString("sex"));
                em.setHomeaddress(rs.getString("homeaddress"));
                em.setEmailaddress(rs.getString("emailaddress"));
                em.setPhonenumber(rs.getString("phonenumber"));
                em.setBirthday(rs.getDate("birthday"));
                em.setIddepartment(rs.getInt("iddepartment"));
                liste.add(em);
            }
        } catch (Exception ex) {
            liste = null;
        }
        return liste;
    }

    public static String ListRole(int a) {
        String liste = "";
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select namedepartment from employee,department where employee.iddepartment=department.iddepartment and employee.idemployee=" + a;
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                liste = rs.getString("namedepartment");
            }
        } catch (Exception ex) {
            liste = null;
        }
        return liste;
    }

    public boolean Login(String username, String password) {
        boolean kq = true;

        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from employee where username=? and password=?";
            PreparedStatement ps = con.prepareStatement(query);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            int dem = 0;
            while (rs.next()) {
                dem++;
            }
            kq = dem == 1;
        } catch (Exception ex) {
            kq = false;
        }

        return kq;
    }

    public int Iddepartment(String username, String password) {
        boolean kq = true;
        int iddepart = 0;
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from Employee where username=? and password=?";
            PreparedStatement ps = con.prepareStatement(query);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                iddepart = rs.getInt("iddepartment");
            }

        } catch (Exception ex) {
            iddepart = 0;

        }

        return iddepart;
    }

    public int getidemploy(String username, String password) {
        int kq = 0;

        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from employee where username=? and password=?";
            PreparedStatement ps = con.prepareStatement(query);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                kq = rs.getInt("idemployee");
            }

        } catch (Exception ex) {
            kq = 0;
        }
        return kq;
    }

    public String getnameemploy(String username, String password) {
        String kq = "";

        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from employee where username=? and password=?";
            PreparedStatement ps = con.prepareStatement(query);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                kq = rs.getString("nameemployee");
            }

        } catch (Exception ex) {
            kq = "";
        }
        return kq;
    }

    public boolean ChangePassword() {
        return CommonQuery.Command("update Employee set password='" + this.password + "'where idemployee=" + this.idemployee);
    }

    public static Vector<Employee> ListIDEONNAMEEMP(String a) {
        Vector<Employee> liste = new Vector<Employee>();
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from employee where nameemployee like '%" + a + "%'" + " order by idemployee desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Employee em = new Employee();
                em.setIdemployee(rs.getInt("idemployee"));
                em.setNameemployee(rs.getString("nameemployee"));
                em.setUsername(rs.getString("username"));
                em.setPassword(rs.getString("password"));
                em.setSex(rs.getString("sex"));
                em.setHomeaddress(rs.getString("homeaddress"));
                em.setEmailaddress(rs.getString("emailaddress"));
                em.setPhonenumber(rs.getString("phonenumber"));
                em.setBirthday(rs.getDate("birthday"));
                em.setIddepartment(rs.getInt("iddepartment"));
                liste.add(em);


            }
        } catch (Exception ex) {
            liste = null;
        }
        return liste;
    }

    public static Vector<Employee> ListIDEONIDDEPART(int iddepart) {
        Vector<Employee> liste = new Vector<Employee>();
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from employee where iddepartment = " + iddepart + " order by idemployee desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Employee em = new Employee();
                em.setIdemployee(rs.getInt("idemployee"));
                em.setNameemployee(rs.getString("nameemployee"));
                em.setUsername(rs.getString("username"));
                em.setPassword(rs.getString("password"));
                em.setSex(rs.getString("sex"));
                em.setHomeaddress(rs.getString("homeaddress"));
                em.setEmailaddress(rs.getString("emailaddress"));
                em.setPhonenumber(rs.getString("phonenumber"));
                em.setBirthday(rs.getDate("birthday"));
                em.setIddepartment(rs.getInt("iddepartment"));
                liste.add(em);


            }
        } catch (Exception ex) {
            liste = null;
        }
        return liste;
    }

    public static String NameDepartment(int a) {
        String name = "";
        Connection con = ConnectToDatabase.getConnection();
        try {
            String query = "select namedepartment from Department, Employee where Employee.iddepartment = Department.iddepartment and  Employee.iddepartment=" + a;
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                name = rs.getString("namedepartment");
            }
        } catch (Exception e) {
            name = null;

        }
        return name;
    }

    public static int LastID() {
        int a = 0;

        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select  idemployee from Employee";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                a = rs.getInt("idemployee");
            }

        } catch (Exception e) {
            System.out.println("Khong lay dc");
        }

        return a;
    }

    public static Vector<Employee> ListIDONUsername(String name) {
        Vector<Employee> listem = new Vector<Employee>();
        int dem = 0;
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from Employee where username like'%" + name + "%' " + " order by idemployee desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                Employee em = new Employee();
                em.setIdemployee(rs.getInt("idemployee"));
                em.setNameemployee(rs.getString("nameemployee"));
                em.setUsername(rs.getString("username"));
                em.setPassword(rs.getString("password"));
                em.setSex(rs.getString("sex"));
                em.setHomeaddress(rs.getString("homeaddress"));
                em.setEmailaddress(rs.getString("emailaddress"));
                em.setPhonenumber(rs.getString("phonenumber"));
                em.setBirthday(rs.getDate("birthday"));
                em.setIddepartment(rs.getInt("iddepartment"));
                listem.add(em);
                dem++;
            }
        } catch (Exception e) {
            listem = null;
            System.out.println("khong co");
        }
        if (dem <= 0) {
            listem = null;
        }
        return listem;
    }
}
